import tkinter
from tkinter import *
from tkinter import filedialog

import cv2
import numpy as np
from PIL import Image, ImageTk

from _02threshold import thresh_operate
from _04smooth import average_blur
from _05corrosion import expand_filter, corrosion_filter
from _06histE import histogram_equal
from _06hist_s import hist_all
import _07laplace
from _08my_fourier import dft_a
from _08flour import fft_spectrum, fft_low
from Canny import Canny

"""
@Project: TorchDL
@Name: mains.py
@Author: linxin_liu
@Date: 2022/11/5 20:57
"""
img_png0 = None
img_png1 = None


def show(filename):
    global img_png0
    img_open0 = Image.open(filename)
    img_png0 = ImageTk.PhotoImage(img_open0)
    lab0 = tkinter.Label(root, image=img_png0, bg='#d3fbfb')
    lab0.place(relx=0.0, rely=0.13, relheight=0.87, relwidth=0.5)


def show_final(name, img):
    global img_png1
    img = Image.fromarray(img)  # 格式转换。
    img_png1 = ImageTk.PhotoImage(img)
    lab1 = tkinter.Label(root, image=img_png1, bg='#e9fbfb')
    lab3.config(text=name + ' successful')
    lab1.place(relx=0.5, rely=0.13, relheight=0.87, relwidth=0.5)


def thresh():  # button0
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = thresh_operate(127, img, 'Binary')
    show_final('thresh', img)


def hist():  # button1 直方图均衡化
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = histogram_equal(img)
    show_final('hist', img)


def hist_s():  # 直方图规范化。
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)
    img = cv2.imread(filename)
    img = hist_all(img)
    show_final('hist_s', img)


def spectrum():  # fft频谱。
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = fft_spectrum(img)
    show_final('spectrum', img)


def low():  # fft低通。
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = fft_low(img)
    show_final('low pass', img)


def c_e():  # 腐蚀和膨胀来边缘检测。
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)

    img_a = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    binary_img = thresh_operate(127, img_a, 'Binary')
    after_cor = corrosion_filter(binary_img, [3, 3])
    img_b = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    binary_img = thresh_operate(127, img_b, 'Binary')
    after_ex = expand_filter(binary_img, [2, 2])
    img = after_ex[:, :] - after_cor[:, :]  # 轮廓

    show_final('c_e', img)


def dft_fre():  # button2
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = dft_a(img)
    show_final('dft_a', img)


def gauss():  # button3
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)  # 展示原始图像。
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = average_blur(img, [3, 3], 'gauss')  # 处理。
    show_final('gauss', img)


def average():  # button4
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)  # 展示原始图像。
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = average_blur(img, [3, 3], 'average')  # 处理。
    show_final('average', img)


def median():  # button5
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)  # 展示原始图像。
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = average_blur(img, [3, 3], 'median')  # 处理。
    show_final('median', img)


def sobel():  # button6
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)  # 展示原始图像。
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = _07laplace.laplace(img, 'sobel_up_down')  # 处理。
    show_final('sobel', img)


def canny():  # button8
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)  # 展示原始图像。
    img = cv2.imread(filename).astype(np.float32)
    edge = Canny(img)
    img = edge.astype(np.uint8)
    show_final('canny', img)


def laplace():  # button9
    filename = filedialog.askopenfilename()  # 完整路径。
    show(filename)  # 展示原始图像。
    img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
    img = _07laplace.laplace(img, 'laplace_around')  # 处理。
    show_final('laplace', img)


root = Tk()
root.title('数字图像处理')
root.state("zoomed")  # 窗口最大化
# root.geometry('1400x740')  # 这里的乘号不是 * ，而是小写英文字母 x


def new():
    s = '新建'
    lb1.config(text=s)


def ope():
    s = '打开'
    lb1.config(text=s)


def sav():
    s = '保存'
    lb1.config(text=s)


def cut():
    s = '剪切'
    lb1.config(text=s)


def cop():
    s = '复制'
    lb1.config(text=s)


def pas():
    s = '粘贴'
    lb1.config(text=s)


def popupmenu(event):
    mainmenu.post(event.x_root, event.y_root)


lb1 = Label(root, text='显示信息', font=('黑体', 32, 'bold'))
lb1.place(relx=0.5, rely=0.5)

mainmenu = Menu(root)

menuBase = Menu(mainmenu)  # 菜单分组 menuBase
mainmenu.add_cascade(label="基本图像处理", menu=menuBase)
menuBase.add_command(label="阈值处理", command=thresh)
menuBase.add_command(label="均值", command=average)
menuBase.add_command(label="高斯", command=gauss)
menuBase.add_command(label="中值", command=median)


menuHist = Menu(mainmenu)  # 菜单分组 menuHist
mainmenu.add_cascade(label="直方图", menu=menuHist)
menuHist.add_command(label="均衡", command=hist)
menuHist.add_command(label="规范化", command=hist_s)

menuFre = Menu(mainmenu)  # 菜单分组 menuFre
mainmenu.add_cascade(label="频域", menu=menuFre)
menuFre.add_command(label="fft频谱", command=spectrum)
menuFre.add_command(label="fft低通", command=low)

menuBorder = Menu(mainmenu)  # 菜单分组 menuBorder
mainmenu.add_cascade(label="边缘检测", menu=menuBorder)
menuBorder.add_command(label="腐蚀膨胀", command=c_e)
menuBorder.add_command(label="canny", command=canny)

menuOther = Menu(mainmenu)  # 菜单分组 menuOther
mainmenu.add_cascade(label="其他", menu=menuOther)
menuOther.add_command(label="pca", command=cut)
menuOther.add_command(label="mean-shift", command=cop())

menuExit = Menu(mainmenu)  # 菜单分组 menuExit
mainmenu.add_cascade(label="退出", menu=menuExit)
menuExit.add_command(label="退出", command=root.destroy)

root.config(menu=mainmenu)
# root.bind('Button-3', popupmenu)  # 根窗体绑定鼠标右击响应事件

# 水平起始位置相对窗体0，垂直起始位置为相对位置0，高是40px，宽0.1。
# button0 = Button(root, text='阈值处理', command=thresh)
# button0.place(relx=0.0, rely=0, relheight=0.065, relwidth=0.1)
# button1 = Button(root, text='直方图均衡', command=hist)
# button1.place(relx=0.1, rely=0, relheight=0.065, relwidth=0.1)
# button2 = Button(root, text='直方图规范化', command=None)
# button2.place(relx=0.2, rely=0, relheight=0.065, relwidth=0.1)
# button3 = Button(root, text='高斯平滑', command=gauss)
# button3.place(relx=0.3, rely=0, relheight=0.065, relwidth=0.1)
# button4 = Button(root, text='均值滤波', command=average)
# button4.place(relx=0.4, rely=0, relheight=0.065, relwidth=0.1)
# button5 = Button(root, text='中值滤波', command=median)
# button5.place(relx=0.5, rely=0, relheight=0.065, relwidth=0.1)
# button6 = Button(root, text='sobel', command=sobel)
# button6.place(relx=0.6, rely=0, relheight=0.065, relwidth=0.1)
# button7 = Button(root, text='fft', command=dft_fre)
# button7.place(relx=0.7, rely=0, relheight=0.065, relwidth=0.1)
# button8 = Button(root, text='canny', command=canny)
# button8.place(relx=0.8, rely=0, relheight=0.065, relwidth=0.1)
# button9 = Button(root, text='laplace', command=laplace)
# button9.place(relx=0.9, rely=0, relheight=0.065, relwidth=0.1)

lab2 = Label(root, text='展示输入图片', bg='#f0fbfb')
lab2.place(relx=0.0, rely=0.065, relheight=0.065, relwidth=0.5)

lab3 = Label(root, text='处理后的图片', bg='#faebfb')
lab3.place(relx=0.5, rely=0.065, relheight=0.065, relwidth=0.5)

root.mainloop()
'''
将其置于主循环中，除非用户关闭，否则程序始终处于运行状态。
执行该程序，一个窗体就呈现出来了。
在这个主循环的根窗体中，
可持续呈现中的其他可视化控件实例，监测事件的发生并执行相应的处理程序。
'''
